package com.hnjowe.outputExcel;

import java.io.BufferedOutputStream;   
import java.io.IOException;   
import java.io.OutputStream;   
import java.text.SimpleDateFormat;
import java.util.Date;
  
import javax.servlet.ServletException;   
import javax.servlet.http.HttpServlet;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  
public class OutputExcel extends HttpServlet{   
 
   private static final long serialVersionUID = 1L;   
    protected void doGet(HttpServletRequest request, HttpServletResponse response)   
            throws ServletException, IOException {   
        this.doPost(request, response);   
    }   
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)   
            throws ServletException, IOException {   
    	String ppa=this.getServletConfig().getServletContext().getRealPath("/");
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String todayDate=df.format(new Date());// new Date()为获取当前系统时间
        int modelId=Integer.parseInt(request.getParameter("modelId"));
        String[] ids=request.getParameterValues("ids");
        HSSFWorkbook wb=null;
        String fileName = "";
        switch(modelId){
            case 1:
            	BasicDetail basicDetail=new BasicDetail();
            	wb=basicDetail.createExcel(ids,ppa);
            	fileName="劳动模范基本情况表（详细）"+todayDate+".xls";
            	break;
            case 2:
            	Basic basic=new Basic();
            	wb=basic.createExcel(ids,ppa);
            	fileName="劳动模范基本情况表"+todayDate+".xls";
            	break;
            case 3:
            	NationLevel nationLevel=new NationLevel();
            	wb=nationLevel.createExcel(ids);
            	fileName="全国劳模'三金'发放情况统计表"+todayDate+".xls";
            	break;
            case 4:
            	ProLevel proLevel=new ProLevel();
            	wb=proLevel.createExcel();
            	fileName="省级劳模'二金'发放情况统计表"+todayDate+".xls";
            	break;
            case 5:
            	CityLevel cityLevel=new CityLevel();
            	wb=cityLevel.createExcel();
            	fileName="部级、市级劳模'劳模援助金'发放情况统计表"+todayDate+".xls";
            	break;
            default:
            	BasicList basicList=new BasicList();
            	wb=basicList.createExcel(ids);
            	fileName="劳模名录一览表"+todayDate+".xls";
        }
           
        fileName = new String(fileName.getBytes("GBK"),"iso8859-1");   
        response.reset();   
        response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下载的文件名   
        response.setContentType("application/vnd.ms-excel");   
        response.setHeader("Pragma", "no-cache");   
        response.setHeader("Cache-Control", "no-cache");   
        response.setDateHeader("Expires", 0);   
        OutputStream output = response.getOutputStream();   
        BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);

           
        try {   
            bufferedOutPut.flush();   
            wb.write(bufferedOutPut);   
            bufferedOutPut.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
            System.out.println( "Output   is   closed "); 
        }
    }   
}  
